package top.yxqz.ck;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Mr.余
 * @date 2022/11/23
 * @package top.yxqz.ck
 */

@WebServlet("/demo3")
public class CookieDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie =  new Cookie("name","yxqz");

        cookie.setMaxAge(60);//默认值是-1 默认就是会话的时间
//        如果参数是0 则cookie失效
//        如果是一个正数，则单位是秒
        cookie.setPath("/"); //设置访问路径
        cookie.setDomain("localhost"); //域名，或者主机地址
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }
}
